package Domain.forumManager;

import java.util.Iterator;
import java.util.Vector;

import objects.User;
import DB.DB;

public class UserManager
{
	DB _database;
	public UserManager(DB database)
	{
		this._database=database;
	}
	
	/**
	 * addUser u to the database
	 * @param u the user to be added
	 */
	public void addUser (User u)
	{
		this._database.addUser(u);
	}
	/**
	 *  crate User , will crate new User with the name has given 
	 * @param nameOfUser the new name of the user to be added to the database
	 */
	public void createUser(String nameOfUser)
	{
		User u=new User (nameOfUser);
		
		this._database.addUser(u);
	}
	
	

	
	
	
	/**
	 * remove User by his/her name
	 * @param nameOfUser the name of the user
	 * #return 1 if the user removed 0 otherwish
	 */
	public int removeUser(String nameOfUser)
	{
		User u=this._database.findUser(nameOfUser);
		if (u==null)
			return 0; 
	
	return removeUser(u);
		
	}
	
	
	
	
	
	
	
	/**
	 * remove user from the database
	 * @param userToBeRemove the user to be removed 
	 */
	public int removeUser(User userToBeRemove)
	{
		this._database.removeUser(userToBeRemove);
		try{
		for (Iterator iterator = _database.getUsers().iterator(); iterator.hasNext();) {
			User user = (User) iterator.next();
			user.removefriend(userToBeRemove);
			
		}
		
		}catch (NullPointerException e)
		{
			return 0;
		}
		return 1;
	}
	
	/**
	 * for tests return the user by is name
	 * @param nameOfUser
	 * @return
	 */
	
	public User getUserByName(String nameOfUser)
	{
		return this._database.findUser(nameOfUser);
	}
	
	
	

	/**
	 * 
	 * @param nameOfUser
	 * @param type 1 for manager , 0 to default user
	 * @return -1 if user doesn't found 1 otherwise
	 */
	public int changeUserType(String nameOfUser,int type)
	{
		User u=_database.findUser(nameOfUser);
		if (u==null)
			return -1;
		if(type==1)
			u.setAsManager();
		if(type==0)
			u.setAsDefaultUser();
		return 1;
	}
	/**
	 * 
	 * @param nameOfUser
	 * @return  -1 if user not found -2 if problem unknown
	 */
	public int getTypeOfUser(String nameOfUser)
	{
		User u=_database.findUser(nameOfUser);
		if (u==null)
			return -1;
		else if (u.getTypeOfUser()==User.Type.MANAGER)
				return 1;
		else if (u.getTypeOfUser()==User.Type.DefaultUser)
			return 0;
		else return -2;
	}
	
	
	
	/**
	 
	 * @param nameOfUser
	 * @param userToBeAdded
	 * @return -1 if one of the users doesn't found , 1 if friend added
	 */
	  
	public int addFreind(String nameOfUser,String friendToBeAdded)
	{
		try{
			_database.findUser(friendToBeAdded).addfriend(_database.findUser(nameOfUser));
			}catch (NullPointerException e)
			{
				return -1;
			}
			return 1;
		
		// the user is register to the freindToBeAdded list
	}
	
	
	
	/**
	 * 
	 * @param nameOfUser
	 * @param friendToBeRemoved
	 * @return @return 1 if friend removed ,-1 if user not found , -2 if userToBeRemoved is not 
	 * friend of nameOfUser
	 */
	public int removeFriend (String nameOfUser,String friendToBeRemoved) 
	{
		
		try{
			return _database.findUser(friendToBeRemoved).removefriend(nameOfUser);
			}catch (NullPointerException e)
			{
				return -1;
			}
			// the user is register to the freindToBeAdded list
	}
	
	
	/**
	 * 
	 * @param nameOfUser
	 * @return all the freinds of the user
	 */
	public Vector <User> getFriendsOfUser(String nameOfUser)
	{
		Vector<User> ans=new Vector<User> ();	
	
		try{
			User u=getUserByName(nameOfUser);
			
			
			Vector <User> tmp=this._database.getUsers();
			for (Iterator iterator = tmp.iterator(); iterator.hasNext();)
			{
				User user = (User) iterator.next();
				if (user.getFrindList().contains(u))
						ans.add(user);
				
			}
		}catch (RuntimeException e)
			{
				return null;
			}
		
		
		return ans;
		
	}
	
	public Vector<User> getAllUsers()
	{

		
		return this._database.getUsers();
	}
	
	
	public Vector<String> getAllUsersAsString()
	{
		Vector<String> ans=new Vector<String>();
		Vector<User> users =this._database.getUsers();
		for (Iterator iterator = users.iterator(); iterator.hasNext();)
		{
			User user = (User) iterator.next();
			ans.add(user.get_nameOfUser());
			
		}
		return ans;
	}
	
}
